<!--
 * @Author: 颜礼田 2564217790@qq.com
 * @Date: 2024-08-26 10:41:26
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-14 09:16:04
 * @FilePath: /webServerAdmin/src/views/system/departmentManage/index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="table-box">
    <ProTable ref="proTableRef" :request-api="fetchDeptTrees" :columns="columns">
      <template #tableHeader>
        <el-button type="primary" v-auth="'System.Department.AddDepartment'" @click="open">新增</el-button>
      </template>
    </ProTable>

    <Edit ref="editRef" @success="refresh"></Edit>
  </div>
</template>

<script setup lang="tsx" name="departmentManage">
import { fetchDeptTrees, removeDept } from "@/api/modules/department";
import ProTable from "@/components/ProTable/index.vue";
import Edit from "./components/edit.vue";
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface";
import { ref } from "vue";
import { RefOpenEventProps } from "@/typings/interface";
import { DepartmentSchema } from "@/schema";
import { ElMessageBox, ElNotification } from "element-plus";
import { ResultEnum } from "@/enums/httpEnum";

const proTableRef = ref<ProTableInstance>();
const editRef = ref<RefOpenEventProps<DepartmentSchema>>();

function open() {
  editRef.value?.open();
}

function edit(row) {
  editRef.value?.open(row);
}

function remove(row) {
  ElMessageBox.confirm("提示", { type: "error", message: "是否删除当前数据？" })
    .then(() => {
      if (row?.id) {
        removeDept({ id: row.id }).then(r => {
          const { code, message } = r;
          if (code == ResultEnum.SUCCESS) {
            ElNotification.success("删除成功！");
            refresh();
          } else {
            ElNotification.error(message);
          }
        });
      }
    })
    .catch(err => {
      console.error(err);
    });
}

function refresh() {
  proTableRef.value?.getTableList();
}

const columns: Array<ColumnProps> = [
  { type: "index", prop: "index", label: "序号", fixed: "left", width: 80 },
  { label: "部门名称", prop: "deptName" },
  { label: "部门编码", prop: "deptCode" },
  {
    label: "操作",
    prop: "action",
    fixed: "right",
    render: ({ row }) => (
      <>
        <el-button type="primary" v-auth="System.Department.EditDepartment" onClick={() => edit(row)} link>
          编辑
        </el-button>
        <el-button type="danger" v-auth="System.Department.RemoveDepartment" onClick={() => remove(row)} link>
          删除
        </el-button>
      </>
    )
  }
];
</script>
